//
//  MMRecoreVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/18.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "MMRecoreVC.h"
#import "RecordTableViewCell.h"

@interface MMRecoreVC ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger _pageIndex;
    NSString *_counterStr;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@end

@implementation MMRecoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    _pageIndex = 1;
    [self.view addSubview:self.tableView];
}
- (void)setPid:(NSString *)pid{
    _pid = pid;
    _pageIndex = 1;
    [self loadData];
}
- (void)loadData{
    
    [NetWorkingManager postWithRequestUrl:@"loadInvestorsByProductId" AndParameters:@{@"parameters":[@{@"pageSize":@"20",@"currentPage":@(_pageIndex),@"productId":_pid} DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {
        if (_pageIndex == 1) {
            self.dataSource = [RecModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
        }else{
            [self.dataSource addObjectsFromArray: [RecModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]]];
        }
    
        _counterStr = [NSString stringWithFormat:@"%@",[[responseObject objectForKey:@"obj"] objectForKey:@"hasCopies"]];
        [self.tableView reloadData];
    } FailBlock:^(NSDictionary *errDic) {
        
    }ShowTips:NO];
}
#pragma mark Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 50;
    }
    return 0.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]init];
    if(section == 0){
        UILabel *headlb = [[UILabel alloc]initWithFont:[UIFont systemFontOfSize:14] textColor:RGBCOLOR(171, 171, 171)];
        UILabel *footlb = [[UILabel alloc]initWithFont:[UIFont systemFontOfSize:14] textColor:RGBCOLOR(171, 171, 171)];
        [headView addSubview:headlb];
        [headView addSubview:footlb];
        headlb.text = [NSString stringWithFormat:@"已有%ld人投资",self.dataSource.count];
        footlb.text = [NSString stringWithFormat:@"投资总额%@元",_counterStr];
        NSRange hRange = [headlb.text rangeOfString:[NSString stringWithFormat:@"%ld",self.dataSource.count]];
        NSRange fRange = [footlb.text rangeOfString:[NSString stringWithFormat:@"%@",_counterStr]];
        
        NSMutableAttributedString *hatr = [[NSMutableAttributedString alloc]initWithString:headlb.text];
        NSMutableAttributedString *fatr = [[NSMutableAttributedString alloc]initWithString:footlb.text];
        [hatr addAttribute:NSForegroundColorAttributeName value:ZZGoldColor range:hRange];
        [fatr addAttribute:NSForegroundColorAttributeName value:ZZGoldColor range:fRange];
        headlb.attributedText = hatr;
        footlb.attributedText = fatr;
        footlb.textAlignment = NSTextAlignmentRight;
        [headlb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headView).offset(15);
            make.centerY.equalTo(headView);
            make.height.equalTo(headView);
        }];
        [footlb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(headView).offset(-15);
            make.centerY.equalTo(headView);
            make.height.equalTo(headView);
        }];
        return headView;
    }else{
 
        return nil;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecordTableViewCell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.section];
    return cell;
}

- (void)viewDidLayoutSubviews{
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.backgroundColor = RGBCOLOR(245, 245, 245);
        [_tableView registerClass:[RecordTableViewCell class] forCellReuseIdentifier:@"RecordTableViewCell"];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            _pageIndex += 1;
            [self loadData];
        }];
        
    }
    return _tableView;
}
@end
